package com.qf.kdxf.util;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSONException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class SpeechIdentifyUtil {
    public static String write(String resp) throws IOException {
        String json1best = null;
        String result = "";
        try {

            cn.hutool.json.JSONObject jsonResponse =  new cn.hutool.json.JSONObject(resp);;
            String orderResult = jsonResponse.getJSONObject("content").getStr("orderResult");

            cn.hutool.json.JSONObject orderResultJson = new cn.hutool.json.JSONObject(orderResult);
            JSONArray latticeArray = orderResultJson.getJSONArray("lattice");

            for (int i = 0; i < latticeArray.size(); i++) {
                JSONObject latticeObj = latticeArray.getJSONObject(i);
                json1best = latticeObj.getStr("json_1best");

                // 输出获取到的文字部分
                System.out.println("测试内容"+json1best);
            }
            // 创建ObjectMapper对象
            ObjectMapper objectMapper = new ObjectMapper();

            // 通过readTree方法解析JSON字符串
            JsonNode rootNode = objectMapper.readTree(json1best);

            for (int i = 0; i < rootNode.get("st").get("rt").get(0).get("ws").size(); i++) {
                String wValue = rootNode.get("st").get("rt").get(0).get("ws").get(i).get("cw").get(0).get("w").asText();

                result += wValue;
            }
            // 获取"w"的值

            // 打印"w"的值
            System.out.println("w的值是：" + result);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result;
    }

}
